package com.dingxin.portal.common.utils;


import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class WebUtils {
	public static final String METHOD_HEAD = "HEAD";

	public static final String METHOD_GET = "GET";

	public static final String METHOD_POST = "POST";

	public static final String HEADER_PRAGMA = "Pragma";

	public static final String HEADER_EXPIRES = "Expires";

	public static final String HEADER_CACHE_CONTROL = "Cache-Control";

	public static final int SESSION_COOKIE = -1;

	public static final int FOREVER_COOKIE = 0x12cc0300;

	public static Cookie cookie(String key, String value, int validy,
			String domainName) {
		if (key != null && value != null) {
			Cookie cookie = new Cookie(key, value);
			cookie.setMaxAge(validy);
			cookie.setPath("/");
			if (domainName != null)
				cookie.setDomain(domainName);
			return cookie;
		} else {
			return null;
		}
	}

	public static Cookie removeableCookie(String key, String domainName) {
		if (key != null) {
			Cookie cookie = new Cookie(key, "");
			cookie.setMaxAge(0);
			cookie.setPath("/");
			if (domainName != null)
				cookie.setDomain(domainName);
			return cookie;
		} else {
			return null;
		}
	}

	public static boolean areEquals(Cookie cookie1, Cookie cookie2) {
		return cookie1.getName().equals(cookie2.getName())
				&& (cookie1.getValue() != null ? cookie1.getValue().equals(
						cookie2.getValue()) : cookie2.getValue() == null)
				&& (cookie1.getMaxAge() == cookie2.getMaxAge()
						&& cookie1.getSecure() == cookie2.getSecure() && cookie1
						.getVersion() == cookie2.getVersion())
				&& (cookie1.getComment() != null ? cookie1.getComment().equals(
						cookie2.getComment()) : cookie2.getComment() == null)
				&& (cookie1.getDomain() != null ? cookie1.getDomain().equals(
						cookie2.getDomain()) : cookie2.getDomain() == null)
				&& (cookie1.getPath() != null ? cookie1.getPath().equals(
						cookie2.getPath()) : cookie2.getPath() == null);
	}

	public static final String getCookieValue(String key, HttpServletRequest req) {
		String str = null;
		Cookie cookies[] = req.getCookies();
		if (null == cookies)
			return null;
		for (int i = 0; i < cookies.length && str == null; i++) {
			Cookie cookie = cookies[i];
			if (cookie.getName().equals(key))
				return cookie.getValue();
		}
		return null;
	}

	public static void applyCacheSeconds(HttpServletResponse response,
			int pageCacheSecond, boolean mustRevalidate) {
		if (pageCacheSecond > 0) {
			// HTTP 1.0 header
			response.setDateHeader(HEADER_EXPIRES, System.currentTimeMillis()
					+ pageCacheSecond * 1000L);
			// HTTP 1.1 header
			String headerValue = "max-age=" + pageCacheSecond;
			if (mustRevalidate) {
				headerValue += ", must-revalidate";
			}
			response.setHeader(HEADER_CACHE_CONTROL, headerValue);
		} else if (pageCacheSecond == 0) {
			response.setHeader(HEADER_PRAGMA, "No-cache");
			// HTTP 1.0 header
			response.setDateHeader(HEADER_EXPIRES, 1L);
			// HTTP 1.1 header: "no-cache" is the standard value,
			// "no-store" is necessary to prevent caching on FireFox.
			response.setHeader(HEADER_CACHE_CONTROL, "no-cache");
			response.addHeader(HEADER_CACHE_CONTROL, "no-store");
		}
	}
	
	public static String parseQueryString(HttpServletRequest request){
		Map map = request.getParameterMap();
		String result = "";
		for(Object o : map.keySet()){
			String key = o+"";
			String[] values = (String[])map.get(key);
			for(String value : values){
				result += key+ "=" + value +"&";
			}
		}
		if(result.length()>0){
			result = result.substring(0, result.length()-1);
		}
		if(result.length()>2048){
			result = result.substring(0, 2048);
		}
		return result;
	}
	protected WebUtils() {
	}
}
